#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <mh/common.h>
#include <mh/sram.h>
#include <mh/mac.h>
#include "knit.h"
#include <mh/mh_file.h>
#include "decript.h"
#include "prefixknit.h"
#include "backpiece.h"


/*
 *
 */
short GetRockZeroCom(MH_KNIT *p)
{
    return p->rock_right_com[0];
}
/*
 *get rock star for every needle(0-15) to right
 */
short GetRockRightStarCom(MH_KNIT *p, uint needle)
{
    short ret = 0;
    if(needle < sizeof(p->rock_right_star_com) / sizeof(short))
    {
        ret = p->rock_right_star_com[needle];
    }
    return ret;
}
/*
 * set rock star for every needle(0-15) to right
 */
int SetRockRightStarCom(MH_KNIT *p, uint needle, short com)
{
    int ret = 0;
    if(needle < sizeof(p->rock_right_star_com) / sizeof(short))
    {
        p->rock_right_star_com[needle] = com;
        if(!needle)
        {
            p->rock_left_star_com[needle] = com;
        }
        if(p->machine_ok)
        {
            //ret = ResetRock(p, 0);
            //if(!ret)
            {
                float fmm = 0.0;
                if(p->inch_type > 0.8)
                {
                    fmm = CM_PER_INCH * 1000.0 / p->inch_type; //0.01mm/needle
                }
                ret = RunRock(p, fmm * needle + p->rock_star + com);
            }
        }
    }
    return ret;
}
/*
 * get rock compensation for every needle(0-15) to right
 */
short GetRockRightCom(MH_KNIT *p, uint needle)
{
    short ret = 0;
    if(needle < sizeof(p->rock_right_com) / sizeof(short))
    {
        ret = p->rock_right_com[needle];
    }
    return ret;
}
/*
 * set rock compensation for every needle(0-15) to right
 */
int SetRockRightCom(MH_KNIT *p, uint needle, short com)
{
    int ret = 0;
    if(needle < sizeof(p->rock_right_com) / sizeof(short))
    {
        p->rock_right_com[needle] = com;
        if(p->machine_ok)
        {
            if(!needle)
            {
                SendSysParam(p);
                ret = ResetRock(p, 0);
            }
            else
            {
                float fmm = 0.0;
                if(p->inch_type > 0.8)
                {
                    fmm = CM_PER_INCH * 1000.0 / p->inch_type; //0.01mm/needle
                }
                ret = RunRock(p, fmm * needle + com);
            }
        }
    }
    return ret;
}
/*
 * get rock tran for every needle(0-15) to right
 */
short GetRockRightTranCom(MH_KNIT *p, uint needle)
{
    short ret = 0;
    if(needle < sizeof(p->rock_right_tran_com) / sizeof(short))
    {
        ret = p->rock_right_tran_com[needle];
    }
    return ret;
}
/*
 * set rock tran for every needle(0-15) to right
 */
int SetRockRightTranCom(MH_KNIT *p, uint needle, short com)
{
    int ret = 0;
    if(needle < sizeof(p->rock_right_tran_com) / sizeof(short))
    {
        p->rock_right_tran_com[needle] = com;
        if(p->machine_ok)
        {
            //ret = ResetRock(p, 0);
            //if(!ret)
            {
                float fmm = 0.0;
                if(p->inch_type > 0.8)
                {
                    fmm = CM_PER_INCH * 1000.0 / p->inch_type; //0.01mm/needle
                }
                short value = fmm * needle;
                if(needle)
                {
                    value += p->rock_right_tran_com[0];
                }
                value += com;
                ret = RunRock(p, value);
            }
        }
    }
    return ret;
}
/*
 * get rock star for every needle(0-15) to left
 */
short GetRockLeftStarCom(MH_KNIT *p, uint needle)
{
    short ret = 0;
    if(needle < sizeof(p->rock_left_star_com) / sizeof(short))
    {
        ret = p->rock_left_star_com[needle];
    }
    return ret;
}
/*
 * set rock star for every needle(0-15) to left
 */
int SetRockLeftStarCom(MH_KNIT *p, uint needle, short com)
{
    int ret = 0;
    if(needle < sizeof(p->rock_left_star_com) / sizeof(short))
    {
        p->rock_left_star_com[needle] = com;
        if(p->machine_ok && needle)
        {
            //ret = ResetRock(p, 0);
            //if(!ret)
            {
                float fmm = 0.0;
                if(p->inch_type > 0.8)
                {
                    fmm = CM_PER_INCH * 1000.0 / p->inch_type; //0.01mm/needle
                }
                ret = RunRock(p, -fmm * needle + p->rock_star + com);
            }
        }
    }
    return ret;
}
/*
 * get rock compensation for every needle(0-15) to left
 */
short GetRockLeftCom(MH_KNIT *p, uint needle)
{
    short ret = 0;
    if(needle < sizeof(p->rock_left_com) / sizeof(short))
    {
        ret = p->rock_left_com[needle];
    }
    return ret;
}
/*
 * set rock compensation for every needle(0-15) to left
 */
int SetRockLeftCom(MH_KNIT *p, uint needle, short com)
{
    int ret = 0;
    if(needle < sizeof(p->rock_left_com) / sizeof(short))
    {
        p->rock_left_com[needle] = com;
        if(p->machine_ok && needle)
        {
            //ret = ResetRock(p, 0);
            //if(!ret)
            {
                float fmm = 0.0;
                if(p->inch_type > 0.8)
                {
                    fmm = CM_PER_INCH * 1000.0 / p->inch_type; //0.01mm/needle
                }
                ret = RunRock(p, -fmm * needle + com);
            }
        }
    }
    return ret;
}
/*
 * get rock tran for every needle(0-15) to left
 */
short GetRockLeftTranCom(MH_KNIT *p, uint needle)
{
    short ret = 0;
    if(needle < sizeof(p->rock_left_tran_com) / sizeof(short))
    {
        ret = p->rock_left_tran_com[needle];
    }
    return ret;
}
/*
 * set rock tran for every needle(0-15) to left
 */
int SetRockLeftTranCom(MH_KNIT *p, uint needle, short com)
{
    int ret = 0;
    if(needle < sizeof(p->rock_left_tran_com)/ sizeof(short))
    {
        p->rock_left_tran_com[needle] = com;
        if(p->machine_ok && needle)
        {
            //ret = ResetRock(p, 0);
            //if(!ret)
            {
                float fmm = 0.0;
                if(p->inch_type > 0.8)
                {
                    fmm = CM_PER_INCH * 1000.0 / p->inch_type; //0.01mm/needle
                }
                short value = -fmm * needle;
                value += p->rock_right_tran_com[0];
                value += com;
                ret = RunRock(p, value);
            }
        }
    }
    return ret;
}
